#pragma once
// 一个异步线程的封装
template<class Function,class Ret,class ... Args>
class ASyncProcess {
    public:
    ASyncProcess(Function t,Ret &ret,Args... args){
        std::cout<<"ASyncProcess construct"<<std::endl;
        mThread = new std::thread([this,t,&ret,args...](){
            try
            {
                ret = t(args...);
                this->m_finish = true;
            }
            catch(const std::exception& e)
            {
                std::cerr << e.what() << '\n';
            }
        });
    }
    ASyncProcess(Function t,Ret &ret){
        std::cout<<"ASyncProcess construct"<<std::endl;
        mThread = new std::thread([this,t,&ret](){
            ret = t();
            mMutex.lock();
            this->m_finish = true;
            mMutex.unlock();
        });
    }    
    bool Finish(){
        return m_finish;
    }
    virtual ~ASyncProcess() {
        mThread->detach();
    }
    private:
        bool m_finish = false;
        std::thread *mThread;
        std::mutex mMutex;
};
